package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;


/**
 * 查看购物车
 */
@WebServlet("/index/cart")
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ITypeService typeService = new TypeServiceImpl();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取HttpSession,判断用户是否登录
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null) {//说明是在没有登录的时候点击的购物车(或者session已经失效,默认有效期是30分钟)
			//重定向到登录页面
			response.sendRedirect("/YoyoShop/index/login");
		}else {
			//获取 [宠物分类] 列表
			List<Type> typeList = typeService.getTypeList();
			//把typeList存入request,供/index/cart.jsp页面使用.
			request.setAttribute("typeList", typeList);
			//请求转发,跳转到/index/cart.jsp
			request.getRequestDispatcher("/index/cart.jsp").forward(request, response);
		}
		
	}

}
